package com.mooc.apigateway.dao;

import com.mooc.apigateway.common.RestResponse;
import com.mooc.apigateway.config.GenericRest;
import com.mooc.apigateway.model.Comment;
import com.mooc.apigateway.model.UserMsg;
import com.mooc.apigateway.utils.Rests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CommentDao {

    @Autowired
    private GenericRest rest;
    @Value("${comment.service.name}")
    private String commentServiceName;

    public List<Comment> getHouseComments(Long houseId) {

        return Rests.exc(() ->{
            String url = Rests.toUrl(commentServiceName, "/comment/getHouseComments?houseId=" + houseId);
            ResponseEntity<RestResponse<List<Comment>>> responseEntity =
                    rest.get(url, new ParameterizedTypeReference<RestResponse<List<Comment>>>() { });
            return responseEntity.getBody();
        }).getResult();
    }


    public void insertUserMsg(UserMsg userMsg) {

        Rests.exc(() -> {
            String url = Rests.toUrl(commentServiceName, "/comment/insertUserMsg");
            ResponseEntity<RestResponse> responseEntity = rest.post(url, userMsg,
                    new ParameterizedTypeReference<RestResponse>() {
                    });
            return responseEntity.getBody();
        });

    }

    public void addHouseComment(Comment comment) {
        Rests.exc(() -> {
            String url = Rests.toUrl(commentServiceName, "/comment/addHouseComment");
            ResponseEntity<RestResponse> responseEntity = rest.post(url, comment,
                    new ParameterizedTypeReference<RestResponse>() {
                    });
            return responseEntity.getBody();
        });
   }



}
